package ru.ssau.university.web.helper;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import ru.ssau.university.persistence.entity.Subject;

public class SubjectConverter implements Converter {
	
	private List<Subject> subjects;
	
	public SubjectConverter(List<Subject> subjects) {
		this.subjects = subjects;
	}

	public Object getAsObject(FacesContext fc, UIComponent arg1, String arg2) {
		for (Subject s: subjects) {
			if (arg2.equals(s.getTitle())) {
				return s;
			}
		}
		return null;
	}

	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		Subject subject = (Subject) arg2;
		return subject.getTitle();
	}
	
}
